"""Setup the CpsWarehouse application"""
import logging
from collections import namedtuple
import csv
import os

from cpswarehouse.config.environment import load_environment
from cpswarehouse.model import meta
from cpswarehouse import model

log = logging.getLogger(__name__)

def setup_app(command, conf, vars):
    """Place any commands to setup cpswarehouse here"""
    load_environment(conf.global_conf, conf.local_conf)

    from cpswarehouse.model import meta
    meta.metadata.bind = meta.engine

    filename = os.path.split(conf.filename)[-1]
    if filename == 'test.ini':
        # Permanently drop any existing tables
        log.info("Dropping existing tables...")
        meta.metadata.drop_all(checkfirst=True)
        
        
    # Create the tables if they don't already exist
    log.info("Creating tables...")
    meta.metadata.create_all(bind=meta.engine)
    log.info("Successfully set up.")

    meta.Session.commit()
    log.info("Successfully set up.")
                                
